package com.hifar.plugins;

import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.BorderStyle;
import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.policy.PictureRenderPolicy;
import com.deepoove.poi.policy.TextRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.util.TableTools;
import com.hifar.plugins.model.TableImgRenderPolicyModel;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;

/**
 * @Description: 表格图片渲染插件
 * @Author zhuWeiWei
 * @Date 2023/5/9 11:21
 */
public class TableImgRenderPolicy extends AbstractRenderPolicy<List<TableImgRenderPolicyModel>> {
    /**
     * 几列
     */
    private int column = 1;

    @Override
    public void doRender(RenderContext<List<TableImgRenderPolicyModel>> context) throws Exception {
        List<TableImgRenderPolicyModel> data = context.getData();
        XWPFRun run = context.getRun();
        if (data != null && !data.isEmpty()) {
            int size = data.size();
            int tableRowNum = size / this.column;
            if (size % this.column != 0) {
                tableRowNum += 1;
            }
            XWPFDocument document = run.getDocument();
            XWPFParagraph paragraph = run.getParagraph();
            XWPFTable table = null;
            if ((paragraph.getBody() instanceof XWPFTableCell)) {
                // 情况1：在表格单元格内创建嵌套表格
                XWPFTableCell parentCell = (XWPFTableCell) paragraph.getBody();
                // 1. 获取单元格的底层XML结构
                CTTc ctTc = parentCell.getCTTc();
                // 2. 在单元格内容中创建新的表格
                CTTbl ctTbl = ctTc.addNewTbl();
                // 3. 创建XWPFTable对象
                table = new XWPFTable(ctTbl, parentCell);

                CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
                width.setType(STTblWidth.DXA);
                width.setW(BigInteger.valueOf(5000)); // 设置一个较大的宽度值

                // 或者使用百分比
                width.setType(STTblWidth.PCT);
                width.setW(BigInteger.valueOf(5000)); // 5000 = 100%


                // 创建行和列
                for (int i = 0; i < tableRowNum; i++) {
                    XWPFTableRow row = table.createRow();
                    // 确保每一行有足够的列
                    for (int j = 0; j < this.column; j++) {
                        if (row.getCell(j) == null) {
                            row.createCell();
                        }
                    }
                }
                table.removeRow(0);
            } else {
                // 当前位置不在表格内部
                table = document.createTable(tableRowNum, this.column);
            }
            BorderStyle borderStyle = BorderStyle.builder().build();
            borderStyle.setType(XWPFTable.XWPFBorderType.NONE);
            TableTools.borderTable(table, borderStyle);
            List<XWPFTableRow> rows = table.getRows();

            Iterator<TableImgRenderPolicyModel> iterator = data.iterator();

            for (XWPFTableRow row : rows) {
                List<XWPFTableCell> cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    if (iterator.hasNext()) {
                        TableImgRenderPolicyModel itemData = iterator.next();
                        if (itemData != null) {
                            PictureRenderData pictureRenderData = itemData.getImgData();
                            String title = itemData.getTitle();
                            // 渲染图片
                            XWPFRun cellRun = cell.getParagraph(cell.getCTTc().getPArray(0)).createRun();
                            XWPFRun cellTitleRun = cell.addParagraph().createRun();
                            PictureRenderPolicy.Helper.renderPicture(cellRun, pictureRenderData);
                            // 渲染图片标题并设置文字居中
                            CTP ctp = cell.getCTTc().getPArray(1);
                            CTPPr ctpPr = ctp.addNewPPr();
                            CTJc ctJc = ctpPr.addNewJc();
                            ctJc.setVal(STJc.CENTER);
                            TextRenderPolicy.Helper.renderTextRun(cellTitleRun, new TextRenderData(title));
                        }
                    }
                }
            }
        }
    }

    @Override
    protected void afterRender(RenderContext<List<TableImgRenderPolicyModel>> context) {
        this.clearPlaceholder(context, true);
    }

    public TableImgRenderPolicy(int column) {
        this.column = column;
    }

    public TableImgRenderPolicy() {
    }
}
